package com.fenghuajueli.module_user.ui.helpcenter

import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.runtime.rememberUpdatedState
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.viewmodel.compose.viewModel
import coil.compose.AsyncImage
import com.fenghuajueli.module_user.R
import com.fenghuajueli.module_user.network.model.helpcenter.HelpCenterData
import com.fenghuajueli.module_user.ui.theme.ModuleDevTheme
import com.fenghuajueli.module_user.ui.widget.HelpCenterBottomWidget
import com.fenghuajueli.module_user.ui.widget.UserTopBarWidget
import com.fenghuajueli.module_user.ui.widget.noRippleClickable

class HelpDetailActivity : ComponentActivity() {
    companion object {
        private const val TAG = "HelpDetailActivity"

        @JvmStatic
        fun start(context: Context, title: String) {
            val starter = Intent(context, HelpDetailActivity::class.java)
                .putExtra(TAG, title)
            context.startActivity(starter)
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            val title = intent.getStringExtra(TAG) ?: ""
            val viewModel = viewModel<HelpCenterViewModel> {
                HelpCenterViewModel(getType(title))
            }
            LaunchedEffect(Unit) {
                if (title == "会员问题")
                    viewModel.initGoods(this@HelpDetailActivity)
            }

            val helpCenter = viewModel.helpCenterData.collectAsState()
            val subscriptionDataState = viewModel.subscriptionState.observeAsState()
            val subscriptionShowState by
                rememberUpdatedState(
                    newValue = subscriptionDataState.value?.any { it.vip_type > 100 } ?: false)


            ModuleDevTheme {
                Surface(
                    modifier = Modifier.fillMaxSize()
                ) {
                    Box(
                        modifier = Modifier
                            .fillMaxWidth()
                            .background(color = MaterialTheme.colorScheme.secondary)
                    ) {
                        Column(
                            modifier = Modifier.fillMaxSize()
                        ) {
                            UserTopBarWidget(
                                primary = MaterialTheme.colorScheme.primary,
                                onPrimary = MaterialTheme.colorScheme.onPrimary,
                                title = title,
                                topPadding = 0.dp,
                                onBackClick = ::finish
                            )

                            Column(
                                modifier = Modifier
                                    .weight(1f)
                                    .verticalScroll(rememberScrollState())
                            ) {
                                Spacer(modifier = Modifier.height(14.dp))
                                helpCenter.value.data_list.forEach {
                                    if (it.question != "如何取消订阅？" || subscriptionShowState)
                                        HelpCenterItem(
                                            modifier = Modifier
                                                .noRippleClickable {
                                                    HelpContentActivity.start(
                                                        this@HelpDetailActivity,
                                                        title,
                                                        it
                                                    )
                                                },
                                            helpCenterItem = it
                                        )
                                }
                            }

                            HelpCenterBottomWidget(
                                modifier = Modifier
                            )
                        }
                    }
                }

            }
        }
    }

    @Composable
    fun HelpCenterItem(
        modifier: Modifier,
        helpCenterItem: HelpCenterData
    ) {
        Row(
            modifier = modifier
                .padding(horizontal = 16.dp, vertical = 6.dp)
                .fillMaxWidth()
                .background(
                    color = MaterialTheme.colorScheme.tertiary,
                    shape = RoundedCornerShape(8.dp)
                )
                .padding(start = 15.dp, end = 12.dp, top = 20.dp, bottom = 20.dp),
            verticalAlignment = Alignment.CenterVertically
        ) {
            Spacer(modifier = Modifier.width(15.dp))

            Text(
                text = helpCenterItem.question,
                fontSize = 15.sp
            )

            Spacer(modifier = Modifier.weight(1f))

            AsyncImage(
                modifier = Modifier
                    .size(16.dp),
                model = R.mipmap.module_user_icon_more,
                contentDescription = null,
                contentScale = ContentScale.FillBounds
            )
        }
    }
}